package com.ld.security.auth.common.sys.vo;

import com.ld.security.auth.common.sys.entity.*;
import com.ld.security.common.core.base.SuperVo;
import com.ld.security.common.core.util.ReflectionUtil;
import com.ld.security.common.mybatis.anno.QueryKey;
import com.ld.security.common.mybatis.anno.SqlKey;
import com.ld.security.common.mybatis.anno.SqlKeys;
import com.ld.security.common.mybatis.base.vo.ProxyVo;
import com.ld.security.common.mybatis.constant.SqlCondition;
import com.ld.security.common.mybatis.util.TreeUtil;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.List;

@Data
public class UserDetailsVo implements SuperVo,ProxyVo {

    private String username;

    @QueryKey
    @ApiModelProperty("用户")
    @SqlKey(name = "username",valueField = "username")
    private SysUserEntity user;


    @SqlKey(name = "id",valueField = "user.depId")
    @QueryKey
    @ApiModelProperty("部门")
    private SysDepEntity dep;


    @SqlKey(name = "userId",valueField = "user.id")
    @QueryKey
    private List<SysRoleUserRelEntity> roleUserRels;


    @SqlKey(name = "depId",valueField = "user.depId")
    @QueryKey
    private List<SysRoleDepRelEntity> roleDepRels;

    @SqlKeys(value = {
            @SqlKey(name = "id", value =SqlCondition.in ,valueField = "roleUserRels.roleId"),
            @SqlKey(and = false,name = "id", value =SqlCondition.in ,valueField = "roleDepRels.roleId")
    })
    @QueryKey
    @ApiModelProperty("角色")
    private List<SysRoleEntity> roles;


    @SqlKey(name = "id", value =SqlCondition.in ,valueField = "roles.permissionIds")
    @QueryKey
    @ApiModelProperty("权限")
    private List<SysPerEntity> pers;



    @ApiModelProperty("系统菜单")
    private List<SysPerEntity> sysMenus;


    @Override
    public void queryAfter() {

        if(ReflectionUtil.isNotNull(pers)){
            sysMenus = TreeUtil.dfsBuildTree(pers);
            TreeUtil.treeSort(sysMenus,(x,y) -> {
                return x.getSortVal() - y.getSortVal();
            });
        }


    }
}
